/*
 * Copyright 2010 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.anim.impl
{

	internal class ParallelAnimation extends AnimationGroupBase
	{

		public function ParallelAnimation(manager:AnimationManager, animations:Array)
		{
			super(manager, animations);
		}

		override protected function get animationName():String
		{
			return "Parallel";
		}

		override protected function onInitializeInternal():void
		{
			var maxDuration:uint = 0;
			var count:uint = _numAnimations;
			var info:AnimationGroupInfo = _animationInfo;

			while (--count > -1)
			{
				info.animation.initialize();
				maxDuration = maxDuration < info.animation._duration ? info.animation._duration : maxDuration;
				info = info.next;
			}

			_duration = maxDuration;
		}

		override protected function onUpdateInternal():void
		{
			var animation:AnimationBase;
			var count:uint = _numAnimations;
			var info:AnimationGroupInfo = _animationInfo;

			while (--count > -1)
			{
				animation = info.animation;
				if (_position === -1 && animation._position !== -1)
				{
					animation.position = -1;
				}
				else if (_position > animation._duration)
				{
					if (animation._position !== animation._duration)
					{
						animation.position = animation._duration;
					}
				}
				else
				{
					animation.position = _position;
				}

				info = info.next;
			}
		}
	}
}
